Move a file

Moves a specified file to a new location, providing the option to specify a new file name.

C# .NET

public static String DoTest()
{
         string strRet = "";
         string path = @"c:\temp\MyTest.txt";
         string path2 = path + "temp";
         try
         {
                  if (!File.Exists(path))
                  {
                           // This statement ensures that the file is created,
                           // but the handle is not kept.
                           FileStream fs = File.Create(path);
                           fs.Close();
                  }
                  // Ensure that the target does not exist.
                  if (File.Exists(path2))
                           File.Delete(path2);
                  // Move the file.
                  File.Move(path, path2);
                  strRet = String.Format("{0} was moved to {1}.\r\n", path, path2);
                  // See if the original exists now.
                  if (File.Exists(path))
                  {
                           strRet = String.Format("The original file still exists, which is unexpected.");
                  }
                  else
                  {
                           strRet = String.Format("The original file no longer exists, which is expected.");
                  }
         }
         catch (Exception e)
         {
                  strRet = String.Format("The process failed: {0}", e.ToString());
         }
         return strRet;         
}

 

Blaze++ .NET

static String DoTest()
{
         String strRet = "";
         String path = "c:\\temp\\MyTest.txt";
         String path2 = path + "temp";
         try
         {
                  if (!File::Exists(path))
                  {
                           // This statement ensures that the file is created,
                           // but the handle is not kept.
                           FileStream fs = File::Create(path);
                           fs.Close();
                  }
                  // Ensure that the target does not exist.
                  if (File::Exists(path2))
                           File::Delete(path2);
                  // Move the file.
                  File::Move(path, path2);
                  strRet = String::Format("{0} was moved to {1}.\r\n", path, path2);
                  // See if the original exists now.
                  if (File::Exists(path))
                  {
                           strRet = String::Format("The original file still exists, which is unexpected.");
                  }
                  else
                  {
                           strRet = String::Format("The original file no longer exists, which is expected.");
                  }
         }
         catch (Exception e)
         {
                  strRet = String::Format("The process failed: {0}", e.ToString());
         }
         return strRet;
}